'''
Created on 25 Mar 2013

@author: yvan
'''
import re
def Find(pat,text):
    match = re.search(pat,text)
    if match:
        print match.group()    
    else:
        print 'not found'
    return match

def Find2(pat,text):
    match = re.search(pat,text)
    if match:
        print match.group(1)
        print match.group(2)
    else: print 'not found'

def Find3(pat,text):
    match = re.findall(pat, text)
    if match:
        print ''
    else: print 'not found'
    return match
def openFile():
    try:
        fread = open("baby1990.html", "r")
        myStrings = fread.readlines()
        return str(myStrings)
    except IOError:
        print "Error: can\'t find file to read in data"
if __name__ == '__main__':
    #Find(r'</h3>','Popularity in 1990</h3>')
    #Find(r'\w[\w.]*@[\w.]+','called yvan.niyo@gmail.com')
    #Find(r'[\w.]+@[\w.]+','called yvan.niyo@gmail.com')
    #Find2(r'([\w.]+)@([\w.]+)','called yvan.niyo@gmail.com')
    #Find3(r'[\w.]+@[\w.]+','called yvan.niyo@gmail.com mac.ndi@yahoo.fr')
    names = []
    
    myfile = openFile() 
    myYear_match = Find(r'Popularity\sin\s(\d\d\d\d)',myfile)
    print myYear_match
    myYear = myYear_match.group(1) 
    print myYear
    names.append(myYear)
    print names
    
    #Retrieve names and ranks
    
    myTuples = Find3(r'<td>(\d+)</td><td>(\w+)</td><td>(\w+)</td>',myfile)
    print myTuples
    myDict_names= {}
    for rank_tuple in myTuples:
        (rank,boyname,girlname)= rank_tuple
        if boyname not in myDict_names:
            myDict_names[boyname]=rank
        if girlname not in myDict_names:
            myDict_names[girlname]=rank
    sorted_names = sorted(myDict_names.keys())
    print sorted_names    
    for name in sorted_names:
        names.append(name + " " + myDict_names[name])    
    print names
    
    
    
    
    
    
    
    
    
        